package controllers;


import models.Profile;
import play.mvc.*;


import static play.libs.Json.toJson;

public class FileYOUController extends Controller {
  	
  public static Result getAbout() {
	  String version = "Version = 2013.04.29 20:00";
	  return ok (toJson(version));		  
  }
	  
	
  public static Result getMetadata() {
	  
	  try {
		  Profile p = Profile.all().get(0);
		  return ok (toJson(Dropbox.getMetadata(p.dpkey, p.dpSecret, null)));
	  } catch (IndexOutOfBoundsException iobe){
		  return badRequest("Error: No account linked");
	  } catch (Exception e){
		  return badRequest(e.getMessage());
	  }
  }
  
  public static Result getUniqueMetadata(String file) {
	  
	  try {
		  
		  Profile p = Profile.all().get(0);
		  file = "/"+ file;
		  return ok (toJson(Dropbox.getMetadata(p.dpkey, p.dpSecret, file)));
		  
	  } catch (IndexOutOfBoundsException iobe){
		  return badRequest("Error: No account linked");
	  } catch (Exception e){
		  return badRequest(e.getMessage());
	  }
  }
  
  public static Result searchFiles(String query) {

	  try {
		  Profile p = Profile.all().get(0);
		  return ok (toJson(Dropbox.search(p.dpkey, p.dpSecret, query, null)));
	  } catch (IndexOutOfBoundsException iobe){
		  return badRequest("Error: No account linked");		  
	  } catch (Exception e){
		  return badRequest(e.getMessage());
	  }
  }
  
  public static Result unLink(){
	  
	  try {
		  
		  Profile p = Profile.all().get(0);
		  p.delete();
		  
	  } catch (IndexOutOfBoundsException iobe){
		  return badRequest("Error deleting profile: No account linked");
	  } catch (Exception e){
		  return badRequest("Error deleting profile:" + e.getMessage());
	  }
	  
	  return ok("To unlink dropbox account, you HAVE TO go to dropbox page and do it mannualy."+
			  "\n\rBut we erased all information about your dropbox account stored in our database");
  }
  
}